﻿// DuiDemo.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "DuiDemo.h"
#include "CMainFrame.h"
#include "ATW.h"


CMainFrame* g_pMainFrame = NULL;
CCtrlLog*   g_logger = NULL;
cmdline::parser g_cmdParser;
vector<MONITORINFO> g_Monitors;

ULONG_PTR				m_gdiplusToken;
Gdiplus::GdiplusStartupInput	m_gdiplusStartupInput;


BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
	DEVMODE dm;
	ZeroMemory(&dm, sizeof(dm));
	dm.dmSize = sizeof(dm);

	//保存显示器信息
	MONITORINFO monitorinfo;
	monitorinfo.cbSize = sizeof(MONITORINFO);

	//获得显示器信息，将信息保存到monitorinfo中
	GetMonitorInfo(hMonitor, &monitorinfo);

	//若检测到主屏
	//if (monitorinfo.dwFlags == MONITORINFOF_PRIMARY)
	//{
	//	if (first)
	//	{
	//		first = FALSE;
	//	}
	//	else
	//	{
	//		first = TRUE;
	//		return FALSE;
	//	}
	//}

	g_Monitors.push_back(monitorinfo);

	return TRUE;
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	g_logger = CCtrlLog::GetLogger("", "orange", 1);

	string sCmdline = WtoA(lpCmdLine);

	if (!sCmdline.empty())
		g_logger->debug("cmdLine: %s", sCmdline.c_str());

	sCmdline = "orange.exe " + sCmdline;
	g_cmdParser.add<string>("file", 'f', "Load local file", false);
	g_cmdParser.add<string>("url", 'u', "Load from URL", false);
	g_cmdParser.add<int>("screen", 's', "which screen to show", false);
	g_cmdParser.add("maxwnd", 'M', "Maximaize Window");
	g_cmdParser.add("multiwnd", 'm', "Multi-Window");
	g_cmdParser.add("clearimg", 'c', "Clear image");
	//g_cmdParser.add("help", '?', "Print usage");
	g_cmdParser.parse_check(sCmdline);

	string usage = g_cmdParser.usage();
	cout << usage.c_str() << endl;

	bool bMaxWnd = false;

	if (g_cmdParser.exist("maxwnd"))
	{
		bMaxWnd = true;
	}

	DString szWndName = _T("Orange DICOM Viewer");

	if (!g_cmdParser.exist("multiwnd"))
	{
		HWND g_hWnd = ::FindWindow(NULL, szWndName);
		if (g_hWnd)
		{
			if (::IsIconic(g_hWnd)) {
				::ShowWindow(g_hWnd, SW_RESTORE);
			}


			if (bMaxWnd) {
				::SendMessage(g_hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
			}

			::SetForegroundWindow(g_hWnd);

			std::string data = sCmdline;
			COPYDATASTRUCT cpd;
			cpd.dwData = 0;
			cpd.cbData = data.length() + 1;
			cpd.lpData = (void*)data.data();
			::SendMessage(g_hWnd, WM_COPYDATA, 0, (LPARAM)&cpd);
			return FALSE;

		}
	}

	GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);

	CPaintManagerUI::SetInstance(hInstance);

#ifdef _DEBUG
	// 资源文件夹
	//CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin\\"));

	// 资源压缩包
	//CPaintManagerUI::SetResourceZip(CPaintManagerUI::GetInstancePath() + _T("skin.zip"));
	
#else
	// 资源ID 或 压缩包
	//CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());

#endif

	HRESULT Hr = ::CoInitialize(NULL);
	if (FAILED(Hr)) 
		return 0;

	CMainFrame* pFrame = new CMainFrame();
	if (pFrame == NULL) 
		return 0;

	int screen = -1;
	RECT rcWork;
	bool bMovetoScreen = false;
	if (g_cmdParser.exist("screen"))
	{
		screen = g_cmdParser.get<int>("screen");
		if (screen > 0)
		{
			EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL);
			int screenIdx = screen - 1;
			if (screenIdx >= 0 && screenIdx < g_Monitors.size())
			{
				MONITORINFO mi = g_Monitors.at(screenIdx);
				if (mi.dwFlags != MONITORINFOF_PRIMARY)
				{
					rcWork = mi.rcWork;
					bMovetoScreen = true;
				}
			}
		}
	}

	g_pMainFrame = pFrame;
	pFrame->SetCmdLine(sCmdline);
	pFrame->Create(NULL, szWndName, UI_WNDSTYLE_FRAME, 0L, 0, 0, 800, 572);

	//pFrame->SetIcon(IDI_ICON_ORA);
	HICON hIcon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_ORA));
	::SendMessage(pFrame->GetHWND(), STM_SETICON, IMAGE_ICON, (LPARAM)(UINT)hIcon);
	
	if (bMovetoScreen)
	{// 显示到指定屏幕
		if (bMaxWnd)
		{
			::MoveWindow(*pFrame, rcWork.left, rcWork.top, rcWork.right - rcWork.left, rcWork.bottom - rcWork.top, TRUE);
		}
		else
		{
			RECT rcDlg = { 0 };
			::GetWindowRect(*pFrame, &rcDlg);
			int DlgWidth = rcDlg.right - rcDlg.left;
			int DlgHeight = rcDlg.bottom - rcDlg.top;

			// Find dialog's upper left based on rcCenter
			int xLeft = (rcWork.left + rcWork.right) / 2 - DlgWidth / 2;
			int yTop = (rcWork.top + rcWork.bottom) / 2 - DlgHeight / 2;
			::SetWindowPos(*pFrame, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
		}
		
	}
	else
	{
		if (bMaxWnd)
			::SendMessage(*pFrame, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
		else
		{
			pFrame->CenterWindow();
			pFrame->ResizeClient();
		}
			
	}
		

	CPaintManagerUI::MessageLoop();

	delete pFrame;
	::CoUninitialize();
	GdiplusShutdown(m_gdiplusToken);
	return 0;
}

